package com.example.andoridclient;

import global.Global;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import model.OrderDetailModel;
import model.OrderModel;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.andoridclient.R.id;

import android.R.integer;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.SimpleAdapter;

public class OrderDetailActivity extends Activity {
	private static final int LOADING = -1;
	private static final int UNLOADING = -2;
	private static final int LAYOUT = -3;

	ProgressDialog progressDialog;

	static OrderModel order;
	static List<OrderDetailModel> details;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_order_detail);
		try {
			order = new OrderModel();
			order.ID = getIntent().getExtras().getInt("orderid");
		} catch (Exception e) {
			finish();
		}
		new Thread(init).start();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.order_detail, menu);
		return true;
	}

	Runnable init = new Runnable() {
		@Override
		public void run() {
			handler.sendEmptyMessage(LOADING);
			String result = Global.Get("Order/GetOrderDetail/" + order.ID);
			try {
				JSONObject orderJ = new JSONObject(result)
						.getJSONObject("Order");
				JSONArray detailJ = new JSONObject(result)
						.getJSONArray("DetailList");

				order.OrderDate = orderJ.getString("OrderDate");
				order.Address = orderJ.getString("Address");
				order.Departure = orderJ.getString("Departure");
				order.Description = orderJ.getString("Description");
				order.Destination = orderJ.getString("Destination");
				order.ExpectDate = orderJ.getString("ExpectDate");
				order.IsFinished = orderJ.getBoolean("IsFinished");
				
				if (!orderJ.isNull("Latitude")) {
					order.Latitude = orderJ.getDouble("Latitude");
				}
				if (!orderJ.isNull("Longitude")) {
					order.Logitude = orderJ.getDouble("Longitude");
				}
				if (!orderJ.isNull("PhoneNumber")) {
					order.PhoneNumber = orderJ.getString("PhoneNumber");
				}
				if (!orderJ.isNull("PostmanName")) {
					order.PostmanName = orderJ.getString("PostmanName");
				}
				order.RecieverName = orderJ.getString("RecieverName");
				order.SecretCode = orderJ.getString("SecretCode");
				order.SendOutName = orderJ.getString("SendOutName");

				details = new ArrayList<OrderDetailModel>();
				for (int i = 0; i < detailJ.length(); i++) {
					JSONObject j = detailJ.getJSONObject(i);
					OrderDetailModel detailModel = new OrderDetailModel();
					detailModel.AcceptTime = j.getString("AcceptTime");
					detailModel.PostmanName = j.getString("PostmanName");
					details.add(detailModel);
				}
				handler.sendEmptyMessage(LAYOUT);
			} catch (JSONException e) {
			}
			handler.sendEmptyMessage(UNLOADING);
		}
	};

	// UI Handler
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case LOADING:
				progressDialog = ProgressDialog.show(OrderDetailActivity.this,
						"请稍等...", "读取中...", true);
				break;
			case UNLOADING:
				progressDialog.dismiss();
				break;
			case LAYOUT:

				break;
			default:
				break;
			}
		}

	};

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
